using System.Collections.Generic;
using System.Linq;
using Dapper;
using LiteTools.service;

namespace LiteTools.domains.db
{
    public class TlvTagFamilyHelper
    {
        public class TlvTagFamily
        {
            public string ttf_id { set; get; }
            public string ttf_name { set; get; }
            public string ttf_desc { set; get; }
        }

        public static bool AddTlvTagFamily(TlvTagFamily tlvTagFamily)
        {
            var connection = DbService.GetConnection();
            if (connection == null)
            {
                return false;
            }

            // 插入示例
            const string insertQuery =
                "INSERT INTO tlv_tag_family (ttf_id, ttf_name, ttf_desc) VALUES (@ID, @NAME, @DESC)";
            var execute = connection.Execute(insertQuery,
                new
                {
                    ID = tlvTagFamily.ttf_id, NAME = tlvTagFamily.ttf_name, DESC = tlvTagFamily.ttf_desc
                });
            var result = execute >= 1;
            connection.Close();
            return result;
        }

        public static bool RemoveTlvTagFamily(string id)
        {
            var connection = DbService.GetConnection();
            if (connection == null)
            {
                return false;
            }

            // 删除示例
            const string deleteQuery = "DELETE FROM tlv_tag_family WHERE ttf_id = @Id";
            var execute = connection.Execute(deleteQuery, new { Id = id });
            connection.Close();
            return execute == 1;
        }

        public static bool UpdateTlvTagFamily(TlvTagFamily tlvTagFamily)
        {
            var connection = DbService.GetConnection();
            if (connection == null)
            {
                return false;
            }

            // 更新示例
            const string updateQuery =
                "UPDATE tlv_tag_family SET ttf_id = @ID, ttf_name = @NAME, ttf_desc = @DESC WHERE ttf_id = @ID";
            var execute = connection.Execute(updateQuery, new
            {
                ID = tlvTagFamily.ttf_id, NAME = tlvTagFamily.ttf_name, DESC = tlvTagFamily.ttf_desc
            });
            connection.Close();
            return execute == 1;
        }

        public static TlvTagFamily GetTlvTagFamilyById(string id)
        {
            var connection = DbService.GetConnection();
            if (connection == null)
            {
                return null;
            }

            const string selectQuery = "SELECT ttf_id, ttf_name, ttf_desc FROM tlv_tag_family WHERE ttf_id = @ID";
            var tlvTagFamilyList = connection.Query<TlvTagFamily>(selectQuery, new { ID = id }).ToList();
            connection.Close();
            return tlvTagFamilyList.Count == 0 ? null : tlvTagFamilyList[0];
        }

        public static List<TlvTagFamily> GetTlvTagFamily()
        {
            var connection = DbService.GetConnection();
            const string selectQuery = "SELECT ttf_id, ttf_name, ttf_desc FROM tlv_tag_family";
            var tlvTagFamilyList = connection.Query<TlvTagFamily>(selectQuery).ToList();
            connection.Close();
            return tlvTagFamilyList;
        }
    }
}